package jee.boot.module.sms.sender;

import jee.boot.common.utils.StringUtils;
import jee.boot.entity.sms.Sms;
import jee.boot.module.sms.interceptor.DefaultSmsPostInterceptor;
import jee.boot.module.sms.interceptor.DefaultSmsPreInterceptor;
import jee.boot.module.sms.interceptor.SmsInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.LinkedList;

/**
 * 邮件发送
 */
@Component
public class DefaultSmsSender{
    @Value("${spring.sms.enable: false}")
    private boolean enable;
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    private LinkedList<SmsInterceptor> preProcessingList = new LinkedList<>();// 前置处理队列
    private LinkedList<SmsInterceptor> postProcessingList = new LinkedList<>();// 后置处理队列
    @Autowired
    private ISmsSender sender;//短信发送接口

    public DefaultSmsSender() {
        addPostInterceptor(new DefaultSmsPostInterceptor());
        addPreInterceptor(new DefaultSmsPreInterceptor());
    }

    public final DefaultSmsSender addPreInterceptor(final SmsInterceptor interceptor) {
        preProcessingList.add(interceptor);
        return this;
    }

    public final DefaultSmsSender addPostInterceptor(final SmsInterceptor interceptor) {
        postProcessingList.add(interceptor);
        return this;
    }

    public LinkedList<SmsInterceptor> getPreProcessingList() {
        return preProcessingList;
    }

    public void setPreProcessingList(LinkedList<SmsInterceptor> preProcessingList) {
        this.preProcessingList = preProcessingList;
    }

    public LinkedList<SmsInterceptor> getPostProcessingList() {
        return postProcessingList;
    }

    public void setPostProcessingList(LinkedList<SmsInterceptor> postProcessingList) {
        this.postProcessingList = postProcessingList;
    }

    public ISmsSender getSender() {
        return sender;
    }

    public void setSender(ISmsSender sender) {
        this.sender = sender;
    }

    /**
     * 邮件发送
     *
     * @param sms
     */
    public void send(Sms sms) {
        if (preProcessingList != null && !preProcessingList.isEmpty()) {
            for (SmsInterceptor pre : preProcessingList) {
                try {
                    pre.process(sms);
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error("发送短信前置处理失败 :" + e.getMessage());
                }
            }
        }
        try {
            if(enable) {
                sender.send(sms);
                sms.setSendStatus("Y");// 发送成功
                sms.setMessage("发送成功");
            }else{
                printSmsLocal(sms);
                sms.setSendStatus("N");// 发送成功
                sms.setMessage("未发送,日志中打印");
            }
        } catch (Exception e) {
            logger.error("发送短信失败 :" + e.getMessage());
            sms.setSendStatus("N");// 发送失败
            sms.setMessage("发送失败"+e.getMessage());
        }
        if (postProcessingList != null && !postProcessingList.isEmpty()) {
            for (SmsInterceptor post : postProcessingList) {
                try {
                    post.process(sms);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (StringUtils.equals("Y", sms.getSendStatus())) {
                        sms.setSendStatus("W");// 发送异常(短信发送成功,但后置处理异常)
                        logger.error("发送短信后置处理异常(邮件发送成功,但后置处理异常) :" + e.getMessage());
                        sms.setMessage("发送异常");
                    }
                    sms.setMessage(sms.getMessage()+e.getMessage());
                }
            }
        }
    }

    /**
     * 本地
     * @param sms
     */
    private void printSmsLocal(Sms sms) {
        logger.info("print sms local: " + sms.toString());
    }

}
